#include <EXTERN.h>
#include <perl.h>

// for document, please find perlembed in perldoc.perl.com
static PerlInterpreter *my_perl;

int pl_start() {
    int exitstatus;
    char *embedding[] = { "Bioteampl", "-e", "0" };

    my_perl = perl_alloc();
    PERL_SET_CONTEXT(my_perl);
     
    perl_construct(my_perl);
    PL_perl_destruct_level = 0;
    
    exitstatus = perl_parse(my_perl, NULL, 3, embedding, (char **)NULL) ;
    exitstatus = perl_run(my_perl);
    
    return 0;
}



char* pl_eval( char* code) {
  SV* ret ;
  STRLEN n_a;
  // Enable the call of the function from multiple Java threads:
  PERL_SET_CONTEXT(my_perl);
  ret = eval_pv( code , 0 ) ;
  
  if ( !SvOK(ret) ) 
  	return NULL ;
  if (ret==NULL)
  	return NULL;
  return SvPV( ret , n_a)  ;
}



char* pl_error() {
  PERL_SET_CONTEXT(PL_curinterp);
  return SvPV( ERRSV , SvLEN(ERRSV) )  ;
}

void pl_stop() {
  PERL_SET_CONTEXT(PL_curinterp);
  perl_destruct(my_perl);
  perl_free(my_perl);
}

